<?php

declare(strict_types=1);

namespace app\group\validate\admin\goods;

use sheep\validate\Backend;

class Goods extends Backend
{
    protected $rule = [
        'category_id'    => 'require',
        'title'          => 'require|max:200',
        'image'          => 'require',
        'original_price' => 'require',
        'is_discount'    => 'require|number',
        'discount'       => 'requireIf:is_discount,1|checkDis',
        'is_limit'       => 'require|number',
        'limit_num'      => 'requireIf:is_limit,1|checkRes',
        'stocks'         => 'number',
        'apply'          => 'max:50',
        'weight'         => 'max:50',
        'product_list'   => 'require|array',
        'explain'        => 'max:2000',
        'validity'       => 'require|gt:0',
        'subscribe'      => 'require|gt:0',
    ];

    protected $message = [
        'category_id.require'    => '请选择服务分类',
        'category_id.number'     => '服务分类必须为整数不包含负数和小数点',
        'title.require'          => '请输入服务名称',
        'title.max'              => '服务名称最多不超过200个字符',
        'image.require'          => '请上传服务主图',
        'original_price.require' => '请输入原价',
        'is_discount.require'    => '请选择折扣',
        'is_discount.number'     => '折扣选项必须为整数不包含负数和小数点',
        'discount.requireIf'     => '折扣价必须填写不能为空',
        'discount.checkDis'      => '折扣价必须大于0折',
        'is_limit.require'       => '请选择限购',
        'is_limit.number'        => '限购选项必须为整数不包含负数和小数点',
        'limit_num.requireIf'    => '每人限购必须填写不能为空',
        'limit_num.checkRes'     => '每人限购填写必须大于0张',
        'stocks.number'          => '库存必须为整数不包含负数和小数点',
        'apply.max'              => '适用宠物最多不超过50个字符',
        'weight.max'             => '适用体重最多不超过50个字符',
//        'weight.checkWeight'     => '适用体重只能输入数字和小数,不能输入中英文',
        'product_list.require'   => '服务项目不能为空',
        'explain.max'            => '项目注明最多不超过2000个字符',
        'product_list.array'     => '服务项目参数有误',
        'validity.require'       => '有效期不能为空',
        'validity.gt'            => '有效期必须大于0天',
        'subscribe.require'      => '预约信息不能为空',
        'subscribe.gt'           => '预约信息必须大于0小时',
    ];


    protected $scene = [
        'add'  => ['category_id', 'title', 'image', 'stocks', 'original_price', 'is_discount', 'discount', 'is_limit', 'limit_num', 'apply', 'weight', 'product_list', 'explain', 'validity', 'subscribe'],
        'edit' => ['category_id', 'title', 'image', 'original_price', 'is_discount', 'discount', 'is_limit', 'limit_num', 'apply', 'weight', 'product_list', 'explain', 'validity', 'subscribe'],
    ];

    // 验证适用体重只能输入数字、破折号和小数，不能输入中英文
    protected function checkWeight($value, $rule, $data)
    {
        return (bool)preg_match('/^[0-9.-]+$/', $value);
    }

    // 验证折扣
    protected function checkDis($value, $rule, $data)
    {
        if ($data['is_discount'] == 1) {
            if ($value > 0) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }

    // 验证每人限购张数
    protected function checkRes($value, $rule, $data)
    {
        if ($data['is_limit'] == 1) {
            if ($value > 0) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }
}
